package org.musicsweet.handler;


import org.musicsweet.exception.ServiceException;
import org.musicsweet.result.ResultData;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;


/**
 * 全局捕获异常类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     *  @ExceptionHandler拦截什么类型的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResultData exceptionHandler(HttpServletRequest req, Exception e) {
        e.printStackTrace();
        return ResultData.createFail("系统异常");
    }

    /**
     * 监听自定义异常处理类
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    public ResultData serviceExceptionHandler(HttpServletRequest req, ServiceException e) {
        e.printStackTrace();
        return ResultData.createFail(e.getErrorMsg());
    }



}
